﻿using BJYJ_Admin.Core;
using Furion;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ReZero;
using ReZero.SuperAPI;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using BJYJ_Admin.Core.DataBaseLog;
using BJYJ_Admin.Core.Utils.ResultUtils;

namespace BJYJ_Admin.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // 获取所有数据库配置
        var dbOptionsxt = App.GetConfig<List<ConnectionConfig>>(path: "ConnectionConfigs");

        services.AddConsoleFormatter();
        //启用全局授权
        services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);

        services.AddCorsAccessor();

        //统一规范化返回结果
        services.AddControllers()
               .AddInjectWithUnifyResult<ResultProvider>();//

        //返回结果属性全小写,应在规范化结果之后注册       
        services.AddControllersWithViews()
                .AddInjectWithUnifyResult()
                .AddJsonOptions(options =>
                {
                    options.JsonSerializerOptions.PropertyNamingPolicy = new LowerCaseNamingPolicy();
                });

        //注入数据库
        services.AddSqlsugarSetup(App.Configuration);
        // ElasticSearch
        services.AddElasticSearch();



        // 系统日志
        services.AddLoggingSetup();

        // 验证码
        services.AddCaptcha();




        //开发环境注册ReZero.Api
        if (!App.HostEnvironment.IsDevelopment())
        {
            services.AddReZeroServices(api =>
            {

                var apiObj = new SuperAPIOptions();

                //Swagger地址
                apiObj.UiOptions.DefaultIndexSource = "/Swagger";
                var firstConnectionConfig = dbOptionsxt.FirstOrDefault();
                apiObj.DatabaseOptions = new DatabaseOptions()
                {
                    ConnectionConfig = new SuperAPIConnectionConfig()
                    {
                        ConnectionString = firstConnectionConfig.ConnectionString,
                        DbType = DbType.Dm
                    }
                };

                //启用超级API
                api.EnableSuperApi(apiObj);

            });
        }

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        // 401 和 403、404 状态码进行规范化处理
        // 添加规范化结果状态码，需要在这里注册
        app.UseUnifyResultStatusCodes(withAuthorizationHeaderCheck: false);
        //以https方式启动
        //app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }
}
